package components;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import nemeanshop.Product;

/**
 *
 * @author Ragnar
 */
public class NProductModel extends AbstractTableModel
{
    private List<Product> products;
    private String colnames[] = { "Varenavn", "Innkjøpspris", "Salgspris", "Antall" };
    
    public NProductModel(List<Product> prods)
    {
        this.products = prods;
        this.genProdList();
        this.sortByQtyLowFirst();
        
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return String.class;
    }

    @Override
    public String getColumnName(int index)
    {
        return (index>=0 && index<colnames.length)
               ? colnames[index].toString()
               :"";
    }

    @Override
    public int getColumnCount()
    {
        return this.colnames.length;
    }

    @Override
    public int getRowCount()
    {
        return this.products.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        Product p = (Product) this.products.get(rowIndex);
        switch(columnIndex)
        {
            case 0:
                return p.getProductName();
            
            case 1:
                return p.getStockPrice();
                
            case 2:
                return p.getSellPrice();
                
            case 3:
                return p.getQty();
                
            default:
                return null;
                   
        }
    }  
    
    public void update(List<Product> p)
    {
        int index = products.size();
        products = p;
        this.genProdList();
        this.sortByQtyLowFirst();
        this.fireTableChanged(new TableModelEvent(this, 0, index, -1, TableModelEvent.ALL_COLUMNS));
    }
    
    public void genProdList()
    {
        ArrayList<Product> list = new ArrayList<Product>();
        for (Product p : this.products)
        {
            list.add(new Product(p.getBarcode(), p.getProductName(), p.getStockPrice(), p.getSellPrice(), p.getQty()));
        }
        this.products = list;
    }
    
    public void sortByQtyLowFirst()
    {
        ArrayList<Product> list = new ArrayList<Product>(this.products.size());
        List<Product> prods = this.products;
        int count = this.products.size();
        int minJ = -1;
        int index = -1;
        for (int i = 0; i < count; i++)
        {            
            for(int j = 0; j < prods.size(); j++)
            {
                if (minJ != -1)
                {
                    if (minJ > prods.get(j).getQty())
                    {
                        minJ = prods.get(j).getQty();
                        index = j;
                    }
                }
                else
                {
                    minJ = prods.get(j).getQty();
                    index = j;
                }
            }
            list.add(i, prods.get(index));
            prods.remove(index);
            minJ = index = -1;
        }
        this.products = list;
        //this.fireTableChanged(new TableModelEvent(this, 0, list.size(), -1, TableModelEvent.ALL_COLUMNS));
    }
    
    public Product getRow(int row)
    {
        return this.products.get(row);
    }
    
}